import Https from '@/utils/https'
import {
  getUserInfo, getCert, projectList, getMenus, getProjectListByUserId, logout,
} from '@/api/global'

export default {
  namespace: 'global',
  state: {
    router: {},
    ids: [],
    user: {},
    inChoose: false,
    themeValue: 'light',
    newProjectLists: [],
    projectLists: [],
    certificate: undefined, // 许可证是否有效
    isLoginLoading: false, // 是否处于登陆后获取各项权限的loading状态
  },
  effects: {
    // ProTable组件获取table数据
    * queryTableData({
      url, method = 'get', params, needCode = false, cb, json = true,
    }, { call }) {
      const reqUrl = () => Https[method](url, params, { needCode, json })
      const data = yield call(reqUrl)
      if (cb) {
        cb(data)
      }
    },
    * getRouter(state, { call, put }) {
      yield put({ type: 'router', state })
    },
    * getUser({ cb }, { call, put }) {
      const data = yield call(getUserInfo)
      yield put({ type: 'user', data })
      if (cb) {
        cb(data)
      }
    },
    * getCert({ cb }, { call, put }) {
      const data = yield call(getCert)
      if (cb) {
        cb(data)
      }
    },
    * getProjectLists(state, { call, put }) {
      const data = yield call(projectList)
      yield put({ type: 'projectLists', data })
      const defaultWorkSpace = sessionStorage.getItem('defaultWorkSpace')
      if (!defaultWorkSpace && data && data.length > 0) {
        sessionStorage.setItem('defaultWorkSpace', JSON.stringify(data[0]))
      }
      yield put({ type: 'isLoginLoading', data: false })
    },
    * changeProjectLists(state, { call, put }) {
      yield put({ type: 'save', payload: { projectLists: state.state.projectLists } })
    },
    * getProjectListsByUserId({ params, cb }, { call, put }) {
      const reqUrl = () => getProjectListByUserId(params)
      const data = yield call(reqUrl)
      const arr = data.result
      // const defaultWorkSpace = sessionStorage.getItem('defaultWorkSpace')
      // if (!defaultWorkSpace && arr && arr.length > 0) {
      //   sessionStorage.setItem('defaultWorkSpace', JSON.stringify(arr[0]))
      // }
      yield put({ type: 'save', payload: { projectLists: arr } })
      yield put({ type: 'isLoginLoading', data: false })
      if (cb) {
        cb(data)
      }
    },
    * getMenus({ params, cb }, { call, put }) {
      const reqUrl = () => getMenus(params)
      const data = yield call(reqUrl)
      if (cb) {
        cb(data)
      }
    },
    * logout({ params, cb }, { call, put }) {
      const res = yield call(logout, params)
      if (cb) {
        cb(res)
      }
    },
    // 更新title
    * updateTitle({ params, cb }, { call, put }) {
      yield put({ type: 'save', payload: params })
    },
    // 表单填写历史记录返回按钮
    * toggleBackBtn({ params, cb }, { call, put }) {
      yield put({ type: 'save', payload: params })
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
    themeChange(state, active) {
      return { ...state, ...active.data }
    },
    routeDarkChoose(state, active) {
      return { ...state, ...active.data }
    },
    routerInChoose(state, active) {
      return { ...state, ...active.data }
    },
    router(state, active) {
      return { ...state, ...active.state.router }
    },
    user(state, active) {
      return { ...state, ...{ user: active.data } }
    },
    projectLists(state, active) {
      return { ...state, ...{ projectLists: active.data, newProjectLists: active.data } }
    },
    certificate(state, active) {
      return { ...state, ...{ certificate: active.data } }
    },
    isLoginLoading(state, active) {
      return { ...state, ...{ isLoginLoading: active.data } }
    },
  },
}
